package 优先算法.递归_搜索与回溯.综合案列.电话号码的字母组合;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String str = "23";
        List<String> result = solution.letterCombinations(str);
        for (String i : result) {
            System.out.println(i);
        }
    }
}
class Solution {
    StringBuffer path;
    List<String> ret;
    String[] hash = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

    public List<String> letterCombinations(String digits) {
        ret = new ArrayList<>();
        path = new StringBuffer();
        if(digits == null || digits.length() == 0) return ret;

        dfs(digits,0);
        return ret;
    }
    public void dfs(String digits,int pos){
        if(pos == digits.length()){
            ret.add(path.toString());
            return;
        }
        String cur = hash[digits.charAt(pos) - '0'];
        for (int i=0 ; i<cur.length();i++){
            path.append(cur.charAt(i));
            dfs(digits,pos+1);
            //恢复
            path.deleteCharAt(path.length() - 1);

        }
    }
}
